package com.cqc.paoclient.ui.mime;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cqc.paoclient.R;
import com.cqc.paoclient.base.BaseFragment;
import com.cqc.paoclient.event.LoginSuccessEvent;
import com.cqc.paoclient.model.BaseMsgModel;
import com.cqc.paoclient.model.MyProfileModel;
import com.cqc.paoclient.ui.login.LoginActivity;
import com.cqc.paoclient.ui.mime.myarticle.MyArticleActivity;
import com.cqc.paoclient.util.GlideUtil;
import com.cqc.paoclient.util.LogUtil;
import com.google.gson.JsonObject;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by cui on 2017/11/22.
 */

public class Tab3Frag extends BaseFragment<Tab3FragPresenter> implements Tab3FragContract.View {

    @BindView(R.id.iv_head)
    ImageView mIvHead;
    @BindView(R.id.tv_nick)
    TextView mTvNick;
    @BindView(R.id.tv_signature)
    TextView mTvSignature;
    @BindView(R.id.tv_fans)
    TextView mTvFans;
    @BindView(R.id.tv_attention)
    TextView mTvAttention;
    @BindView(R.id.bt_logout)
    Button mBtLogout;
    private boolean isLogin = false;


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
    }

    /**
     * 接收到了LoginSuccessEvent事件
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(LoginSuccessEvent event) {
        mPresenter.getMyProfile();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.frag_tab_3;
    }

    @Override
    protected Tab3FragPresenter getmPresenter() {
        return new Tab3FragPresenter();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void loadData() {
        LogUtil.d("loadData");
        mPresenter.getMyProfile();
    }

    @Override
    protected void presenterSetView() {
        mPresenter.setView(this);
    }


    //------------以下4个方法是实现接口BaseView中的方法----------------------------
    @Override
    public void getMyProfileSucc(MyProfileModel myProfileModel) {
        if (myProfileModel == null || myProfileModel.getModel() == null) {
            showToat("未登录或登录已过期");
            return;
        }
        //设置个人信息
        isLogin = true;
        GlideUtil.loadHeadImage(myProfileModel.getModel().getFace()).into(mIvHead);
        mTvNick.setText(myProfileModel.getModel().getNickname());
        mTvSignature.setText(myProfileModel.getModel().getQianming());
        mTvFans.setText(String.valueOf(myProfileModel.getModel().getFans()));
        mTvAttention.setText(String.valueOf(myProfileModel.getModel().getGuanzhu()));
        mBtLogout.setVisibility(View.VISIBLE);
    }

    @Override
    public void getMyProfileFail(String msg) {
        showToat(msg);
        mBtLogout.setVisibility(View.GONE);
    }

    @Override
    public void getLogOutSucc(BaseMsgModel model) {
        if (model.getSucess() == 1) {
            clearMyProfile();
        } else {
            showToat(model.getMessage());
        }
    }

    @Override
    public void getLogOutFail(String msg) {

    }

    @OnClick({R.id.iv_head, R.id.rl_article, R.id.rl_collection, R.id.rl_msg, R.id.rl_set, R.id.bt_logout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_head:
                startActivityWithIntent(LoginActivity.class);
                break;
            case R.id.rl_article:
                if (isLogin) {
                    startActivityWithIntent(MyArticleActivity.class);
                } else {
                    startActivityWithIntent(LoginActivity.class);
                }
                break;
            case R.id.rl_collection:
                if (isLogin) {
                } else {
                    startActivityWithIntent(LoginActivity.class);
                }
                break;
            case R.id.rl_msg:
                showToat("TODO");
                break;
            case R.id.rl_set:
                showToat("TODO");
                break;
            case R.id.bt_logout:
                mPresenter.loginOut();
                break;
        }
    }
    //------------以上4个方法是实现接口BaseView中的方法----------------------------


    /**
     * 清空个人信息
     */
    private void clearMyProfile() {
        isLogin = false;
        mIvHead.setImageResource(R.drawable.ic_default_head);
        mTvNick.setText(R.string.click_login);
        mTvSignature.setText(R.string.no_signature);
        mTvFans.setText(R.string.zero);
        mTvAttention.setText(R.string.zero);
        mBtLogout.setVisibility(View.GONE);
    }
}
